<?php
require_once 'init.php';
include 'isValidToken.php';
require_once 'Dingbot.php';
if(!$logined){
	echo "登陆失败";
	return;
}
$client = new GuzzleHttp\Client([
            'timeout' => 5.0,
        ]);
//伪装HTTP头
$headers=[
'User-Agent'=>'Mozilla/5.0 (Linux; U; Android 4.1.2; zh-cn; GT-I9300 Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 MicroMessenger/5.2.380',
'Authorization'=>$Authorization,
'Connection'=>'keep-alive',
'Host'=>' courseapi.ulearning.cn',
'Origin'=>' https://courseweb.ulearning.cn',
'Accept'=>'application/json, text/javascript, */*; q=0.01',
'Sec-Fetch-Dest'=>'empty',
'Content-Type'=>'application/json',
'Sec-Fetch-Site'=>'same-site',
'Sec-Fetch-Mode'=>'cors',
'Referer'=>'https://courseweb.ulearning.cn/ulearning/index.html',
'Accept-Encoding'=>'gzip, deflate, br',
'Accept-Language'=>'zh-CN,zh;q=0.9',
];
include_once'homeworklist.php';
	foreach($homeworklist as $id=>$name){
	$res = $client->request('GET', "https://courseapi.ulearning.cn/homeworks/student?ocId=$id&pn=1&ps=10", [
           
            'headers' => $headers
			
        ])->getBody()->getContents();
		
		$data=json_decode($res,true);
		if(!empty($data['homeworkList'])){
			foreach($data['homeworkList'] as $item){
			if ($item['endTime']>msectime()){
				if($item['state']<3){
					$endTime = msecdate($item['endTime']);
				echo "$name ".$item['homeworkTitle']." 截止时间 $endTime 未完成\n";
				}else{
					$endTime = msecdate($item['endTime']);
				echo "$name ".$item['homeworkTitle']." 截止时间 $endTime 已完成\n";
				}
				
				}
			}
		}
		
	
}

/* 毫秒时间戳转换成日期 */
function msecdate($time)
{
$tag='Y-m-d H:i:s';
   $a = substr($time,0,10);
   $b = substr($time,10);
   $date = date($tag,$a).'.'.$b;
   return $date;
}

//返回当前的毫秒时间戳
function msectime()
 {
     list($msec, $sec) = explode(' ', microtime());
           $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
           return $msectime;
}
